package com.ukao.cashregister.printer;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.argox.sdk.barcodeprinter.BarcodePrinter;
import com.argox.sdk.barcodeprinter.connection.PrinterConnection;
import com.argox.sdk.barcodeprinter.emulation.pplz.PPLZ;
import com.argox.sdk.barcodeprinter.emulation.pplz.PPLZBarCodeType;
import com.argox.sdk.barcodeprinter.emulation.pplz.PPLZOrient;
import com.argox.sdk.barcodeprinter.util.Encoding;
import com.example.tscdll.TSCUSBActivity;
import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseActivity;
import com.ukao.cashregister.eventbus.MembershipCardEvent;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.ukao.cashregister.R.id.argoxPrint;
import static com.ukao.cashregister.eventbus.MembershipCardEvent.Message.print;


/**
 * USB打印机连接回调类
 */
public class PrinterConnTipActivity extends BaseActivity    {
    @BindView(R.id.tipInfo)
    TextView mTipInfo;
    @BindView(R.id.tsc244PrintTest)
    Button mTsc244PrintTest;
    @BindView(R.id.tsc247PrintTest)
    Button mTsc247PrintTest;
    @BindView(R.id.exitPrinterTip)
    Button mExitPrinterTip;
    protected BarcodePrinter<PrinterConnection, PPLZ> printer;
    PrinterHelper mPrinterHelper = null;
    private UsbDevice usbDevice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer_conn_tip);
        ButterKnife.bind(this);
        mPrinterHelper = PrinterHelper.getInstance();
         usbDevice= (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if(usbDevice != null) {
            L.i("FileName: "+ usbDevice.getDeviceName());
            L.i("ProductId: "+ usbDevice.getProductId());
            L.i("VendorId: "+ usbDevice.getVendorId());
             if (usbDevice.getVendorId() == 34918 && usbDevice.getProductId() == 256) {
                Map  map=new HashMap();
                map.put(ARG_PARAM1,Utils.getContext().getResources().getString(R.string.receipt_code));
                map.put(ARG_PARAM2,Utils.getContext().getResources().getString(R.string.receipt_print));
                MembershipCardEvent.postHasData(print,map);
            }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 294){/*水洗*/
                Map  map=new HashMap();
                map.put(ARG_PARAM1, Utils.getContext().getResources().getString(R.string.wash_code));
                map.put(ARG_PARAM2, Utils.getContext().getResources().getString(R.string.wash_print));
                MembershipCardEvent.postHasData(print,map);

            }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 294) {/*立象*/
                 Map  map=new HashMap();
                 map.put(ARG_PARAM1, Utils.getContext().getResources().getString(R.string.argox_code));
                 map.put(ARG_PARAM2, Utils.getContext().getResources().getString(R.string.argox_print));
                 MembershipCardEvent.postHasData(print,map);
             }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 370) {/*TSC清单打印*/
                 Map  map=new HashMap();
                 map.put(ARG_PARAM1, Utils.getContext().getResources().getString(R.string.tag_code));
                 map.put(ARG_PARAM2, Utils.getContext().getResources().getString(R.string.tag_print));
                 MembershipCardEvent.postHasData(print,map);
             }else if(usbDevice.getVendorId() == 4611 && usbDevice.getProductId() == 310) {/*TSC T-210E清单打印*/
                 Map  map=new HashMap();
                 map.put(ARG_PARAM1, getString(R.string.detailed));
                 map.put(ARG_PARAM2, Utils.getContext().getResources().getString(R.string.tag_print));
                 MembershipCardEvent.postHasData(print,map);

             }


        }else{

            T.show("未检测到连接设备");
        }
        finish();

    }

    @Override
    protected void initView() {

    }

    @OnClick({R.id.tsc244PrintTest, R.id.tsc247PrintTest, R.id.exitPrinterTip, argoxPrint})
    public void onViewClicked(View view) {
        PrintInfo printInfo = new PrintInfo();
        switch (view.getId()) {
            case R.id.tsc244PrintTest:

                printInfo.mGoodsName = "腰带";
                printInfo.mOrderNum = "UW20170504338178";
                printInfo.mTotalCount = "11";
                printInfo.mReciveClothesDatetime = "05-04 14:34";
                printInfo.mClothesBarcode = "18042036127164";
                printInfo.mShopName = "麓云店";
                printInfo.mShopNum = "145";
                printInfo.mTransportNum = "22";
                PrinterHelper.getInstance().printTsc244(printInfo);
                break;
            case R.id.tsc247PrintTest:
                printInfo.mGoodsName = "羽绒服（短）";
                printInfo.mOrderInfo = "UW20170509610458 清洗 ￥19.00";
                printInfo.mCurrentIndex = "1";
                printInfo.mTotalCount = "6";
                printInfo.mAttachGoods = "附件 领子/帽子";
                printInfo.mAttachInfo = "黑色/磨损/色斑";
                printInfo.mAttachService = "无";
                printInfo.mShopName = "麓云店";
                printInfo.mShopNum = "145";
                printInfo.mTransportNum = "47";
                printInfo.mTransportName = "13507916555万田";
                printInfo.mClothesBarcode = "1453814194229";
               // mPrinterHelper.printTsc247(printInfo);
                break;
            case R.id.exitPrinterTip:
                finish();
                break;
            case argoxPrint:
                try {
                    Encoding encode = Encoding.UTF_8;
                    byte[] buf, buf2;
                    buf = encode.getBytes("23456");

                    printer.getEmulation().getBarcodeUtil().printOneDBarcode(50, 50, PPLZOrient.Clockwise_0_Degrees, 5, 13, 50,
                            PPLZBarCodeType.Code_39, 1, buf, 'N', 'Y', 'N', 'N', 'N');// Code 39
                    buf2 = encode.getBytes("01234567890123456789");
                    printer.getEmulation().getBarcodeUtil().printOneDBarcode(50, 200, PPLZOrient.Clockwise_0_Degrees, 5, 13, 50,
                            PPLZBarCodeType.Code_128, 1, buf, 'Y', 'N', 'N', 'N', 'N');// Code 128
                    buf2 = encode.getBytes(">9123$%&");
                    printer.getEmulation().getBarcodeUtil().printOneDBarcode(50, 350, PPLZOrient.Clockwise_0_Degrees, 5, 13, 50,
                            PPLZBarCodeType.Code_128, 1, buf2, 'Y', 'N', 'N', 'N', 'N');// Code 128, Subsets A(Numeric Pairs give Alpha/Numerics)
                    printer.getEmulation().getSetUtil().setLabelLength(500);
                    printer.getEmulation().getSetUtil().setPrintOut(1, 0, 1, false);

                } catch (Exception ex) {
                    Log.e("argox_demo", null, ex);
                }

                break;


        }
    }


}
